/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

int times = 0;
osTimerId_t tid;
#define FLAGS_MSK1 0x00000001U
#define FLAGS_MSK2 0x00000010U
#define FLAGS_MSK 0x00000011U

osEventFlagsId_t evt_id;

void timer_thread(void *arg) {
    (void)arg;
    times++;
    printf("Time's up %d.\r\n", times);
    osEventFlagsSet(evt_id, FLAGS_MSK1);
        if(times%24 >= 8 && times%24 <= 18){
        printf("The sun's out.\r\n");
        osEventFlagsSet(evt_id, FLAGS_MSK2);
    }

}

void feed_thread(void *arg) {
 (void)arg;
  while (1) {
    osEventFlagsWait(evt_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
    printf("Feed the dog.\r\n");
  }
}

static void TestTask(void)
{

    tid = osTimerNew(timer_thread, osTimerPeriodic, NULL, NULL);
    osTimerStart(tid, 100);
    evt_id = osEventFlagsNew(NULL);

    osThreadAttr_t attr;
    attr.name = "feed_thread";
    attr.stack_size = 1024*4;
    attr.priority = osPriorityNormal;
    osThreadId_t tid1 = osThreadNew(feed_thread, NULL, &attr);

    osDelay(3000);
    osTimerStop(tid);
    osTimerDelete(tid);
}
APP_FEATURE_INIT(TestTask);